
import { _decorator, AnimationClip, Component, Animation, Node, assetManager, CCString, CCInteger, instantiate } from 'cc';
import { Loader } from './LoadManger';
import { GameData } from '../system/GameData';
import { MonsterManager } from '../fight/monster/MonsterManager';
import { MonsterNormal } from '../fight/monster/MonsterNormal';
import { DataHelper } from '../system/DataHelper';
import { resManger } from './ResManger';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = test
 * DateTime = Fri May 19 2023 16:59:08 GMT+0800 (GMT+08:00)
 * Author = guoxun
 * FileBasename = test.ts
 * FileBasenameNoExtension = test
 * URL = db://assets/script/tools/test.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */

@ccclass('test')
export class test extends Component {
    @property({
        type: String,
    }) // 警告：不应该使用构造函数 String。等价于 CCString。也可以选择不声明类型
    mstring = '';

    private interval = 0.1;
    private curTime = 0;
    private sfs = null;
    private isStart = false;
    protected onLoad(): void {
        GameData.InitPlayData();
        this.InitGame(() => {
            new MonsterManager(this.node);
            this.isStart = true;
        })
    }

    InitGame(callbcak) {
        GameData.InitPlayData();
        assetManager.loadBundle("dynamicPrefab", (err, data) => {
            Loader.dynamicPrefab = data;
            resManger.InitJson(() => {
                assetManager.loadBundle("dynamic", (err, data) => {
                    Loader.dynamic = data;
                    callbcak();
                })
            });

        })
    }

    private loadMonster() {
        let an: Animation = this.node.getComponent(Animation);
        assetManager.loadBundle("dynamic", (err, data) => {
            Loader.dynamic = data;
            Loader.LoadMonsterImageDir(this.mstring + "/run", (arr) => {
                const clip = AnimationClip.createWithSpriteFrames(arr, 20);
                clip.name = "run";
                clip.wrapMode = AnimationClip.WrapMode.Loop;
                clip.enableTrsBlending = true;
                an.addClip(clip);
                this.node.getComponent(Animation).play("run");
            })
        })
    }

    private loadEffect() {
        let an: Animation = this.node.getComponent(Animation);
        assetManager.loadBundle("dynamic", (err, data) => {
            Loader.dynamic = data;
            Loader.LoadEffectImageDir(GameData.mainGunlevelUpAn, (arr) => {
                if (!arr) return;
                const clip = AnimationClip.createWithSpriteFrames(arr, arr.length);
                clip.name = "death";
                clip.wrapMode = AnimationClip.WrapMode.Normal;
                an.addClip(clip);
                this.node.getComponent(Animation).play("death");
            })
        })
    }

    private loadMuzzleFlash() {
        let an: Animation = this.node.getComponent(Animation);
        Loader.LoadMuzzleImageDir("dianCiPaoFire", (arr) => {
            const clip = AnimationClip.createWithSpriteFrames(arr, arr.length);
            clip.name = "death";
            clip.wrapMode = AnimationClip.WrapMode.Normal;
            an.addClip(clip);
            this.node.getComponent(Animation).play("death");
        })
    }

    private loadSkill() {
        let an: Animation = this.node.getComponent(Animation);
        Loader.LoadSkillImageDir("dianCiPaoFire", (arr) => {
            this.sfs = arr;
            const clip = AnimationClip.createWithSpriteFrames(arr, arr.length);
            clip.name = "run";
            clip.wrapMode = AnimationClip.WrapMode.Loop;
            an.addClip(clip);
        })
        this.PlayDeath();
    }

    PlayDeath() {
        if (!this.sfs) {
            this.schedule(this.PlayDeath, 0.1);
            return;
        }
        //this.ndAnimation.getComponent(UITransform).contentSize = this.ltRunAnSpfs[0].originalSize;
        this.unschedule(this.PlayDeath);
        this.node.getComponent(Animation).play("run")

    }

    private AddMonster() {
        let id = 100001;
        let monsterConfig = DataHelper.GetMonsterBaseConfigById(id);
        let refreshConfig = DataHelper.GetMonsterRefreshConfigById(10002);
        Loader.LoadPrefab("Monster", (prefab) => {
            let node = instantiate(prefab);
            node.addComponent(MonsterNormal).InitMonster(refreshConfig, monsterConfig);
            this.node.addChild(node);
        })
    }

    private AddMonster1() {
        let id = 100002;
        let monsterConfig = DataHelper.GetMonsterBaseConfigById(id);
        let refreshConfig = DataHelper.GetMonsterRefreshConfigById(10002);
        Loader.LoadPrefab("Monster", (prefab) => {
            let node = instantiate(prefab);
            node.addComponent(MonsterNormal).InitMonster(refreshConfig, monsterConfig);
            this.node.addChild(node);
        })
    }

    protected update(dt: number): void {
        if (!this.isStart) return;
        this.curTime += dt;
        if (this.curTime > this.interval) {
            this.curTime = 0;
            this.AddMonster();
            this.AddMonster1();
        }
    }
}

